package com.example.zhinongbao.manager;

import android.content.Context;

import com.example.zhinongbao.R;
import com.example.zhinongbao.model.Product;

import java.util.ArrayList;
import java.util.List;

public class ProductManager {
    private static ProductManager instance;
    private List<Product> allProducts;

    public static ProductManager getInstance(Context context) {
        if (instance == null) {
            instance = new ProductManager(context);
        }
        return instance;
    }

    private ProductManager(Context context) {
        // 从数据库加载所有商品（实际项目中应实现）
        allProducts = loadAllProductsFromDB();

        // 开发阶段使用模拟数据
        if (allProducts.isEmpty()) {
            allProducts = createDemoProducts();
        }
    }

    private List<Product> loadAllProductsFromDB() {
        // 实际实现：通过ContentResolver查询商品表
        List<Product> products = new ArrayList<>();
        // 这里添加数据库查询逻辑
        return products;
    }

    private List<Product> createDemoProducts() {
        List<Product> products = new ArrayList<>();

//        // 添加首页的商品（图一）
        products.add(new Product(1, "有机苹果", 12.50, R.drawable.apple,
                "新鲜有机苹果", "山东烟台"));
        products.add(new Product(2, "绿色蔬菜", 8.00, R.drawable.vegetable,
                "无农药绿色蔬菜", "北京顺义"));
        products.add(new Product(3, "农家土鸡蛋", 15.00, R.drawable.egg,
                "散养土鸡蛋", "河北承德"));
        products.add(new Product(4, "生态大米", 6.50, R.drawable.rice,
                "原生稻种植大米", "黑龙江五常"));

        return products;
    }

    public Product getProductById(int id) {
        for (Product product : allProducts) {
            if (product.getId() == id) {
                return product;
            }
        }
        return null;
    }

    public List<Product> getAllProducts() {
        return allProducts;
    }
}